package yja.gzw.app.activity

import android.content.Intent
import android.view.View
import android.widget.Toast
import androidx.lifecycle.Observer
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.tencent.imsdk.v2.V2TIMConversation
import com.tencent.qcloud.tuicore.TUIConstants
import com.tencent.qcloud.tuikit.tuichat.classicui.page.TUIC2CChatActivity
import yja.gzw.app.R
import yja.gzw.app.base.BaseActivity
import yja.gzw.app.databinding.ActivityDindanchangajiexiangqingBinding
import yja.gzw.app.model.UserData
import yja.gzw.app.viewmodel.Mainviewmodel
import java.text.SimpleDateFormat
import java.time.Instant
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.util.Date
import java.util.Locale
import java.util.TimeZone

class DindanchangajiexiangqingActivity :BaseActivity<Mainviewmodel, ActivityDindanchangajiexiangqingBinding>(
    R.layout.activity_dindanchangajiexiangqing){
    var userid="0"
    override fun initView() {
            val ids =intent.getStringExtra("ids")
        val zhungtai =intent.getIntExtra("zhuangtai",0)
       binding.qufabu.setOnClickListener {
           val intent=Intent(this,QufabuActivity::class.java)
           intent.putExtra("idss",ids)
           startActivity(intent)

       }
        when(zhungtai){
            0->{
                binding.zitizhanshi.text="未支付"
            }
            1->{
                binding.zitizhanshi.text="发布中"
            }
            2->{
                binding.zitizhanshi.text="待确认"
            }
            3->{
                binding.zitizhanshi.text="待发货"
            }
            4->{
                binding.zitizhanshi.text="待收货"
            }
            5->{
                binding.zitizhanshi.text="待评价"
            }
            -1->{
                binding.zitizhanshi.text="撤销订单"
            }

        }
        viewmodel.onxiangqingwang(UserData.token, mapOf("order_id" to ids!!))

        binding.sixing.setOnClickListener {
            val intent = Intent(this, TUIC2CChatActivity::class.java)
            intent.putExtra(TUIConstants.TUIChat.CHAT_ID,userid);
            intent.putExtra(TUIConstants.TUIChat.CHAT_TYPE, V2TIMConversation.V2TIM_C2C)
            startActivity(intent)
        }

        binding.quxiao.setOnClickListener {
            viewmodel.onchengjiefangquxiaodingdanwang(UserData.token, mapOf("order_id" to ids))

        }
    }

    override fun dataObservese() {
        viewmodel.onchengjiefangquxiaodingdan.observe(this, Observer {
            if(it.code==1){
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
                finish()
            }else{
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
            }

        })


        viewmodel.onxiangqing.observe(this, Observer {
            if(it.code==1){
            binding.biaoti.text=it.data.label

                if(it.data.type=="1"){
                    binding.zhuanquleixing.text="三元视频"

                }else if(it.data.type=="2"){
                    binding.zhuanquleixing.text="四元视频"
                }else{
                    binding.zhuanquleixing.text="精品视频"
                }
                binding.fabumoshioqian.text="已出价￥${it.data.pay_price}"

                binding.wengzisucaishow.text=it.data.text_material
                val requestOptions = RequestOptions.bitmapTransform(CircleCrop())
//                Glide.with(this).load("http://117ai.32.hzgqapp.com/"+it.data.publisher.avatar)
//                    .apply(requestOptions)
//                    .into(binding.touxiang)
//                binding.name.text=it.data.publisher.username
                binding.dingdanbiaohao.text=it.data.order_no
                val timestamp = 1739169752L
                // 将秒级时间戳转换为 Instant 对象
                //todo 爆红不必理会
                val instant = Instant.ofEpochSecond(timestamp)

                val zoneId = ZoneId.systemDefault()
                // 将 Instant 对象转换为 LocalDateTime 对象
                val localDateTime = instant.atZone(zoneId).toLocalDateTime()
                // 定义日期时间格式化模式
                val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
                // 格式化日期时间
                val formattedDateTime = localDateTime.format(formatter)
               binding.dindanfabushijian.text=formattedDateTime
                binding.dindanjiage.text="￥${it.data.pay_price}"

                userid="${it.data.publisher.id}"


            }else{
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
            }

        })


    }
}